/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "http2_napi.h"
#include <string>


EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        {"connect", nullptr, Http2Napi::Connect, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"request", nullptr, Http2Napi::Request, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"write", nullptr, Http2Napi::Write, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"end", nullptr, Http2Napi::End, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"dnsResolveTxt", nullptr, Http2Napi::DnsResolveTxt, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"dnsLookup", nullptr, Http2Napi::DnsLookup, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"openNativeLog", nullptr, Http2Napi::OpenNativeLog, nullptr, nullptr, nullptr, napi_default, nullptr}
    };
    napi_value httpNapi = nullptr;
    const char *classBindName = "Http2Napi";
    int methodSize = std::end(desc) - std::begin(desc);
    napi_define_class(env, classBindName, strlen(classBindName), Http2Napi::ClassConstructor, nullptr, methodSize, desc,
                      &httpNapi);
    napi_set_named_property(env, exports, "Http2Napi", httpNapi);
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module initModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "http2_napi",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterModule(void)
{
    napi_module_register(&initModule);
}